﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace CaoNiMaCoolPad
{
    class Utility
    {
        public static string ParseUnicode(byte[] message, int position, int length)
        {
            int p = position;
            while (p + 1 < message.Length && (message[p] != 0 || message[p + 1] != 0) && p < position + length)
                p += 2;

            char[] cs = Encoding.Unicode.GetChars(message, position, p - position);
            return new string(cs);
        }

        public static bool TryParseTime(byte[] message, int position, out string result)
        {
            StringBuilder sb = new StringBuilder();
            int value = message[position + 1] * 16 * 16 + message[position];
            if (value < 1990 || value > 2030)
                goto Error;
            sb.Append(value.ToString());//年
            sb.Append("年");
            position += 4;

            value = message[position];
            if (value > 12)
                goto Error;
            sb.Append(value.ToString());
            sb.Append("月");
            position += 4;

            value = message[position];
            if (value > 31)
                goto Error;
            sb.Append(value.ToString());
            sb.Append("日");
            position += 4;

            value = message[position];
            if (value > 24)
                goto Error;
            sb.Append(value.ToString());
            position += 4;
            sb.Append(':');

            value = message[position];
            if (value > 60)
                goto Error;
            sb.Append(value.ToString());
            position += 4;
            sb.Append(':');

            value = message[position];
            if (value > 60)
                goto Error;
            result = sb.Append(value.ToString()).ToString();
            return true;

        Error:
            result = string.Empty;
            return false;
        }

        public static string ByteArrayToString(byte[] bytes, int index, int length)
        {
            if (length > 5)
            {
                StringBuilder sb = new StringBuilder(length);
                for (int i = 0; i < length; i++)
                    sb.Append(bytes[index + i].ToString("X2") + " ");
                sb.Remove(sb.Length - 1, 1);
                return sb.ToString();
            }
            else
            {
                string str = string.Empty;
                int i;
                for (i = 0; i < length - 1; i++)
                    str += bytes[index + i].ToString("X2") + " ";
                str += bytes[index + i].ToString("X2");
                return str;
            }
        }

        public static int IndexOf4Zero(byte[] bytes, int index = 0)
        {
            Contract.Requires(bytes != null);
            Contract.Requires(0 <= index);
            Contract.Ensures(Contract.Result<int>() == -1 ||
                             (Contract.OldValue(index) <= Contract.Result<int>() && Contract.Result<int>() < bytes.Length));

            int zeroCount = 0;
            for (; index < bytes.Length; index++)
            {
                if (bytes[index] == 0)
                {
                    zeroCount++;
                    if (zeroCount == 4)
                        return index - 3;
                }
                else
                    zeroCount = 0;
            }
            return -1;
        }
    }
}
